package cn.cloudtogo.adaptation.amqp;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yaolianhua789@gmail.com
 **/
@Configuration
@Slf4j
@EnableConfigurationProperties(ExternalRabbitmqProperties.class)
@EnableRabbit
public class RabbitmqConfiguration {

    public static final String QUEUE_MIDDLEWARE_UPDATE = "queue-middleware-update";
    public static final String QUEUE_MIDDLEWARE_DELETE = "queue-middleware-delete";
    public static final String EXCHANGE_IRONEGG_SPECIFIC_CHANGE = "ironegg-specific-change";

    @Bean
    public Queue messageSubscribeQueueMiddlewareUpdate() {
        return QueueBuilder.durable(QUEUE_MIDDLEWARE_UPDATE).build();
    }

    @Bean
    public Queue messageSubscribeQueueMiddlewareDelete() {
        return QueueBuilder.durable(QUEUE_MIDDLEWARE_DELETE).build();
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.declareQueue(messageSubscribeQueueMiddlewareUpdate());
        rabbitAdmin.declareQueue(messageSubscribeQueueMiddlewareDelete());

        log.info("Created queue: {},{}", QUEUE_MIDDLEWARE_UPDATE, QUEUE_MIDDLEWARE_DELETE);
        return rabbitAdmin;
    }
}
